package com.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.server.UID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exception.DAOSystemException;
import model.CompensationLeaveDTO;

import bizlogic.CompensationLeaveManager;


/* ************************************************************** */
/* Servlet implementation to load data for overtime entry form    */
/* Added by : Suganthi V Subramaniam                              */
/* *************************************************************  */

public class OTProcessServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;

	public OTProcessServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
		String pbalanceOT="";
		String puserid="";
		pbalanceOT = (String)request.getParameter("balanceOT");
		//String ptotalOT = "10";
		
		
		
		puserid= (String) request.getParameter("userid");
		//String pcompensationid= "1";
		
		
		CompensationLeaveManager clm = new CompensationLeaveManager();
		CompensationLeaveDTO cldto =clm.findCompensationLeave(puserid);
		
			
		if ( cldto.getId() !=null)
		{
	
			cldto.setBalanceOT(Double.parseDouble(pbalanceOT));
			try {
				clm.updateCompensationLeaveOT(cldto);
				request.setAttribute("msg", "SUCCESSFUL!!");
			} catch (DAOSystemException e) {
				// TODO Auto-generated catch block
				request.setAttribute("msg", "FAILED!!");
			}
		}
		else
		{
			cldto.setId(puserid);
			cldto.setBalanceOT(Double.parseDouble(pbalanceOT));
			cldto.setConsumeOT(0.0);
			cldto.setTotalOT(0.0);
			try {
				clm.insertCompensationLeave(cldto);
				request.setAttribute("msg", "SUCCESSFUL!!");
			} catch (DAOSystemException e) {
				// TODO Auto-generated catch block
				request.setAttribute("msg", "FAILED!!");
			}
		}
		
		
		RequestDispatcher rd = request.getRequestDispatcher("/OverTimeEntryForm.jsp");
		rd.forward(request, response);
		
	}
}
